package keeto.com.vn.classified.ui;

import java.io.File;

import keeto.com.vn.classified.utils.ImageUtilities;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;


/**
 * 
 * @author hector
 *
 */
public class ChooseImagePreviewActivity extends Activity implements OnClickListener{

	private static final String ACTION_CHOOSE_IMAGE = "com.supersabona.im.ui.activity.ChooseImagePreviewActivity.ChooseImage";
	private static final String ACTION_TAKE_PHOTO = "com.supersabona.im.ui.activity.ChooseImagePreviewActivity.TakePhoto";
	private static final String ACTION_CHOOSE_BACKGROUND = "com.supersabona.im.ui.activity.ChooseImagePreviewActivity.ChooseBackground";

	private static final int CAMERA_RESULT = 1;
	private static final int PICK_FROM_CALLERY = 2;

	private ImageView ivBackground = null;

	private ImageButton ibSave;
	private ImageButton ibRotateLeft;
	private ImageButton ibRotateRight;
	private ImageButton ibCancel;

	private String mAction = "";

	private int requestCode = -1;

	private Bitmap mBitmap = null;
	private String mFilePath = "";
	private Uri mUri = null;

	public static void actionChoosePicture(Activity context, int requestCode) {
		Intent intent = new Intent(context, ChooseImagePreviewActivity.class);
		intent.setAction(ACTION_CHOOSE_IMAGE);
		context.startActivityForResult(intent, requestCode);
	}

	public static void actionTakePhoto(Activity context, int requestCode) {
		Intent intent = new Intent(context, ChooseImagePreviewActivity.class);
		intent.setAction(ACTION_TAKE_PHOTO);
		context.startActivityForResult(intent, requestCode);
	}
	
	public static void actionChooseBackgroundLib(Activity context, int requestCode,String path) {
		Intent intent = new Intent(context, ChooseImagePreviewActivity.class);
		intent.setAction(ACTION_CHOOSE_BACKGROUND);
		intent.putExtra("path", path);
		context.startActivityForResult(intent, requestCode);
	}
	

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.choose_image_preview_activity);

		ivBackground = (ImageView) findViewById(R.id.image);
		ibSave = (ImageButton) findViewById(R.id.save);
		ibRotateLeft = (ImageButton) findViewById(R.id.rotate_left);
		ibRotateRight = (ImageButton) findViewById(R.id.rotate_right);
		ibCancel = (ImageButton) findViewById(R.id.cancel);

		ibSave.setOnClickListener(this);
		ibRotateLeft.setOnClickListener(this);
		ibRotateRight.setOnClickListener(this);
		ibCancel.setOnClickListener(this);
		
		Intent intent = getIntent();
		mAction = intent.getAction();
		if (mAction.equals(ACTION_CHOOSE_IMAGE)) {

			Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK,
					MediaStore.Images.Media.INTERNAL_CONTENT_URI);
			startActivityForResult(getImageFromGalleryIntent, PICK_FROM_CALLERY);

		} else if (mAction.equals(ACTION_TAKE_PHOTO)) {

			doTakePhotoAction();

		}else if (mAction.equals(ACTION_CHOOSE_BACKGROUND)){
			Intent it  = getIntent();
			mFilePath = it.getStringExtra("path");
			mBitmap = ImageUtilities.getBitmapThumbnailFromFile(mFilePath,
					MainApplication.getScreenWidth(), MainApplication.getScreenHeight());
			ivBackground.setImageBitmap(mBitmap);
		}

	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.save:
			//save image
			ImageUtilities.saveToPath(mBitmap, mFilePath);
			//callback the activity
			Intent data = new Intent();
			data.putExtra("filepath", mFilePath);
			setResult(RESULT_OK, data);
			finish();
			break;
		case R.id.rotate_left:
			mBitmap = ImageUtilities.rotate(mBitmap, -90, null);
			ivBackground.setImageBitmap(mBitmap);
			break;
		case R.id.rotate_right:
			mBitmap = ImageUtilities.rotate(mBitmap, 90, null);
			ivBackground.setImageBitmap(mBitmap);
			break;
		case R.id.cancel:
			finish();
			break;
		}
	}
	

	private void doTakePhotoAction() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ "/pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg";

		mUri = Uri.fromFile(new File(mFilePath));
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

		try {
			intent.putExtra("return-data", true);
			startActivityForResult(intent, CAMERA_RESULT);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK) {
			finish();
			return;
		}

		switch (requestCode) {
		case CAMERA_RESULT:

			mFilePath = mUri.getPath();
			mBitmap = ImageUtilities.getBitmapThumbnailFromFile(mFilePath,
					MainApplication.getScreenWidth(), MainApplication.getScreenHeight());
			ivBackground.setImageBitmap(mBitmap);
			break;

		case PICK_FROM_CALLERY: 
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			if(cursor !=null) {
				cursor.moveToFirst();

				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				mFilePath = cursor.getString(columnIndex);
				cursor.close();
			}else {
				mFilePath = selectedImage.getPath();
			}
			
			mBitmap = ImageUtilities.getBitmapThumbnailFromFile(mFilePath,
					MainApplication.getScreenWidth(), MainApplication.getScreenHeight());
			ivBackground.setImageBitmap(mBitmap);
			
			break;
		}

	}



}
